package com.cskaoyan.service;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.MarketIssueExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Since 2022/12/28 22:39
 * @Author LA
 * @Description:IssueService具体实现类
 */
@Service
public class IssueServiceImp implements IssueService {
    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public CommonData<MarketIssue> list(BasePageInfo info, String question) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        // 只根据question 做条件查询 where question like？
        MarketIssueExample example = new MarketIssueExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketIssueExample.Criteria criteria = example.createCriteria();

        // 查询逻辑存在,表中deleted字段值为1是删除状态
        criteria.andDeletedEqualTo(false);

        // question 先判空
        if (StringUtil.isEmpty(question)) {
            // 空的时候默认匹配全部了
            criteria.andQuestionLike("%" + "" + "%");
        } else {
            // 不空则会按条件模糊查询
            criteria.andQuestionLike("%" + question + "%");
        }

        // 调用mapper中的方法返回 相应类型的数据
        List<MarketIssue> list = issueMapper.selectByExample(example);
        // 获取分页信息
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(list);
        // 填充data返回
        return CommonData.data(pageInfo);

    }

    @Override
    public void create(MarketIssue marketIssue) {
        issueMapper.insertSelective(marketIssue);
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        // 获得 要删除的问题的id
        Integer id = marketIssue.getId();
        // 获得 要删除的问题的逻辑删除标志
        Boolean deleted = marketIssue.getDeleted();
        // 通过id 更新 删除标记
        MarketIssue issueDelete = new MarketIssue();
        //(1,,,,,,1)
        issueDelete.setId(id);
        // 删除标记取反
        issueDelete.setDeleted(!deleted);
        issueMapper.updateByPrimaryKeySelective(issueDelete);
    }

    @Override
    public MarketIssue update(MarketIssue marketIssue) {
        // 获取要更新的数据的 id
        Integer id = marketIssue.getId();
        // 创建新数据对象,并填新值
        MarketIssue issueUpdate = new MarketIssue();
        issueUpdate.setId(id);
        // 更新,更新时间
        issueUpdate.setUpdateTime(new Date());
        // 更新,问题和回复
        issueUpdate.setQuestion(marketIssue.getQuestion());
        issueUpdate.setAnswer(marketIssue.getAnswer());
        issueMapper.updateByPrimaryKeySelective(issueUpdate);

        return issueUpdate;

    }
}
